package com.cat.model.web.action.admin.bas;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.BasService;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.util.Constant;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.BasMapper;
import com.cat.model.web.action.base.AdminAction;

public class BasConfigManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private BasConfigVO basConfig;
	private Long id;
	private String name;
	private String nameLabel;
		
	@Autowired
	private BasService basService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private BasMapper basMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(name)){
			condition.put("name", name);
		}
		
		if(StringUtils.isNotEmpty(nameLabel)){
			condition.put("nameLabel", nameLabel);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchBasConfigSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<BasConfigVO> list = (List<BasConfigVO>)plmCommDAO.search("manager.searchBasConfig", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		basConfig = basMapper.getBasConfig(id);
		if(null == basConfig){
			parentAlert(false, "基础配置不存在，请刷新列表");
			return null;
		}
		
		if(Constant.SYSTEM_FLAG_FALSE.equals(basConfig.getEditFlag())){
			parentAlert(false, "该基础配置不可编辑");
			return null;
		}
		
		basService.deleteBasConfig(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		basService.createBasConfig(basConfig);
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		basConfig = basMapper.getBasConfig(id);
		return "edit";
	}
	
	public String edit(){
		basService.updateBasConfig(basConfig);
		parentAlert(true, "保存成功");
		return "list";
	}

	public BasConfigVO getBasConfig() {
		return basConfig;
	}

	public void setBasConfig(BasConfigVO basConfig) {
		this.basConfig = basConfig;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNameLabel() {
		return nameLabel;
	}

	public void setNameLabel(String nameLabel) {
		this.nameLabel = nameLabel;
	}
	
}
